﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Windows;
using System.Diagnostics;
using System.Collections;
using System.Linq;

namespace Xceed.Silverlight.DataGrid
{
  internal class VisitorBase : IVisitor
  {

    public VisitorBase( ChildrenPolicy childrenPolicy, bool fixedHeadersFooters)
    {
      m_childrenPolicy = childrenPolicy;
      m_fixedHeadersFooters = fixedHeadersFooters;
    }

    public bool IsStopped
    {
      get
      {
        return m_stopVisit;
      }
    }

    public virtual void Visit( DataGridPanel dataGridPanel )
    {
      if( m_fixedHeadersFooters )
      {
        Action<UIElement> visitHeaderFooter = ( element ) =>
        {
          IVisitorElement visitorChild = element as IVisitorElement;
          if( visitorChild != null )
          {
            visitorChild.Accept( this );
          }
        };

        foreach( UIElement element in dataGridPanel.FixedHeaders )
        {
          if( m_stopVisit )
            break;

          visitHeaderFooter( element );
        }

        foreach( UIElement element in dataGridPanel.FixedFooters )
        {
          if( m_stopVisit )
            break;

          visitHeaderFooter( element );
        }
      }

      dataGridPanel.RootHost.Accept( this );
    }

    public virtual void Visit( WrapperBase wrapper )
    {
      if( m_stopVisit )
        return;

      IVisitorElement child = wrapper.Child as IVisitorElement;
      if( child != null )
      {
        child.Accept( this );
      }
    }

    public virtual void Visit( RowWrapper wrapper )
    {
      this.Visit( ( WrapperBase )wrapper );
    }

    public virtual void Visit( Row row )
    {
    }

    public virtual void Visit( DataRow dataRow )
    {
      this.Visit( ( Row )dataRow );
    }

    public virtual void Visit( ColumnManagerRow row )
    {
      this.Visit( ( Row )row );
    }






    public virtual void Visit( VirtualRootContainer container )
    {
      this.Visit( ( VirtualContainerBase )container );
    }

    public virtual void Visit( VirtualGroupContainer container )
    {
      this.Visit( ( VirtualContainerBase )container );
    }

    public virtual void Visit( VirtualContainerBase containerBase )
    {
      IEnumerable elements = ( m_childrenPolicy != ChildrenPolicy.AllChildren )
        ? containerBase.RenderedElements
        : containerBase.HeadersFooters.Cast<ILayoutElement>();

      if( elements != null )
      {
        foreach( object elem in elements )
        {
          if( m_stopVisit )
            break;

          ( ( IVisitorElement )elem ).Accept( this );
        }
      }
    }

    public virtual void Visit( RootHost rootHost )
    {
      if( m_childrenPolicy == ChildrenPolicy.RenderedChildrenOnly 
        || m_childrenPolicy == ChildrenPolicy.LocalyHandled )
      {
        rootHost.RootContainer.Accept( this );
      }
      else
      {
        Debug.Assert( m_childrenPolicy == ChildrenPolicy.AllChildren );
        rootHost.RootContainer.Accept( this );
        foreach( ILayoutElement elem in rootHost.ChildrenManager.AllElements.ToList() )
        {
          if( m_stopVisit )
            break;

          ( ( IVisitorElement )elem ).Accept( this );
        }
      }
    }

    internal void StopVisit()
    {
      m_stopVisit = true;
    }


    private ChildrenPolicy m_childrenPolicy;
    private bool m_fixedHeadersFooters;
    private bool m_stopVisit; //false
  }
}
